package callnumber.gtdev5.com.photoVault.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.service.notification.NotificationListenerService;
import android.support.annotation.Nullable;
import android.util.Log;

import com.junruy.fakecall.KeepLiveAidlInterface;

import java.util.List;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.bean.CallCustomBean;
import callnumber.gtdev5.com.photoVault.utils.ServiceCheckUtils;
import callnumber.gtdev5.com.photoVault.utils.WakeAndLockUtils;

public class LockService extends Service{
    private MediaPlayer mediaPlayer;
    private static int GRAY_SERVICE_ID = 1001;
    private IBinder binder;
    private ServiceConnection connection;
    private Intent intent;
    private WakeAndLockUtils utils;
    private AlarmManager manager;
    private PendingIntent pendingIntent;
    List<CallCustomBean> datas;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (Build.VERSION.SDK_INT < 18) {
            startForeground(GRAY_SERVICE_ID, new Notification());
            stopForeground(true);
        } else {
            if (Build.VERSION.SDK_INT<26){
                Intent intent1 = new Intent(this, LockService.GrayInnerService.class);
                startService(intent1);
                startForeground(GRAY_SERVICE_ID, new Notification());
                stopForeground(true);
            }

        }

        CheckAndStartOtherService();

        //播放无声音乐  针对oppo  华为 6.0以上系统
        if (Build.VERSION.SDK_INT >= 23) {
            if (mediaPlayer!=null){
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                    mediaPlayer.setVolume(0f, 0f);
                }
            }

        }

        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
       // EventBus.getDefault().register(this);
        utils=new WakeAndLockUtils(this);
        //播放无声音乐  针对oppo  华为 6.0以上系统
        if ( Build.VERSION.SDK_INT >= 23) {
            mediaPlayer = MediaPlayer.create(this, R.raw.equal);
            if (mediaPlayer!=null){
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.start();
                    }
                });
            }

        }
        initOtherServices();


 
    }



    private void initOtherServices() {
        binder = new KeepLiveAidlInterface.Stub.Stub() {
            @Override
            public String getServiceName() throws RemoteException {
                return "LockService";
            }
        };
        if (Build.VERSION.SDK_INT >= 21) {
            intent = new Intent(this, JobHandlerService.class);
        } else if (Build.VERSION.SDK_INT >= 18) {
            intent = new Intent(this, NotificationListenerService.class);
        }
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.e("zeoy", "【主锁服务绑定成功】");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.e("zeoy", "【守护停止   -  启动  -  守护】");
                startService(intent);
                bindService(intent, connection, BIND_IMPORTANT);
            }
        };
    }
 
    private void CheckAndStartOtherService() {
        // 5.0以上系统  开启jobservice 保活
        if (Build.VERSION.SDK_INT >= 21) {
            if (!ServiceCheckUtils
                    .isServiceWorked(this,
                            "callnumber.gtdev5.com.fakecall_4.service.JobHandlerService")) {
                startService(intent);
            }
            bindService(intent, connection, BIND_IMPORTANT);

            //4.3以上  开启通知监听服务保活
        } else if (Build.VERSION.SDK_INT >= 18) {

            intent = new Intent(this, NotificationListenerService.class);
            if (!ServiceCheckUtils
                    .isServiceWorked(this,
                            "callnumber.gtdev5.com.fakecall_4.service.JobHandlerService")) {
                Log.e("zeoy", "启动18以上的服务");
                startService(intent);
            }
            Log.e("zeoy", "绑定18以上的服务");
            bindService(intent, connection, BIND_IMPORTANT);
        }
    }



    public static class GrayInnerService extends Service {

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            startForeground(GRAY_SERVICE_ID, new Notification());
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }
   

    @Override
    public void onDestroy() {
      if (Build.VERSION.SDK_INT >= 18)
            unbindService(connection);
        super.onDestroy();
        //启动自己
        startService(new Intent(this, LockService.class));
        bindService(intent, connection, BIND_IMPORTANT);
    }

}
